package com.david.xlib.widget.bgarefresh;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ScrollView;

/**
 */
public class ScrollingUtil {  private ScrollingUtil() {
}

    public static boolean isScrollViewOrWebViewToTop(View view) {
        return view != null && view.getScrollY() == 0;
    }

    public static boolean isAbsListViewToTop(AbsListView absListView) {
        if(absListView != null) {
            int firstChildTop = 0;
            if(absListView.getChildCount() > 0) {
                firstChildTop = absListView.getChildAt(0).getTop() - absListView.getPaddingTop();
            }

            if(absListView.getFirstVisiblePosition() == 0 && firstChildTop == 0) {
                return true;
            }
        }

        return false;
    }

    public static boolean isRecyclerViewToTop(RecyclerView recyclerView) {
        if(recyclerView != null) {
            int firstChildTop = 0;
            if(recyclerView.getChildCount() > 0) {
                ViewGroup.MarginLayoutParams manager = (ViewGroup.MarginLayoutParams)recyclerView.getChildAt(0).getLayoutParams();
                firstChildTop = recyclerView.getChildAt(0).getTop() - manager.topMargin - recyclerView.getPaddingTop();
            }

            RecyclerView.LayoutManager manager1 = recyclerView.getLayoutManager();
            if(manager1 == null) {
                return true;
            }

            if(manager1.getItemCount() == 0) {
                return true;
            }

            if(manager1 instanceof LinearLayoutManager) {
                LinearLayoutManager layoutManager = (LinearLayoutManager)manager1;
                if(layoutManager.findFirstCompletelyVisibleItemPosition() < 1 && firstChildTop == 0) {
                    return true;
                }
            } else if(manager1 instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager layoutManager1 = (StaggeredGridLayoutManager)manager1;
                int[] out = layoutManager1.findFirstCompletelyVisibleItemPositions((int[])null);
                if(out[0] < 1) {
                    return true;
                }
            }
        }

        return false;
    }

    public static boolean isStickyNavLayoutToTop(BGAStickyNavLayout stickyNavLayout) {
        return isScrollViewOrWebViewToTop(stickyNavLayout) && stickyNavLayout.isContentViewToTop();
    }

    public static boolean isWebViewToBottom(WebView webView) {
        return webView != null && (float)webView.getContentHeight() * webView.getScale() == (float)(webView.getScrollY() + webView.getMeasuredHeight());
    }

    public static boolean isScrollViewToBottom(ScrollView scrollView) {
        if(scrollView != null) {
            int scrollContentHeight = scrollView.getScrollY() + scrollView.getMeasuredHeight() - scrollView.getPaddingTop() - scrollView.getPaddingBottom();
            int realContentHeight = scrollView.getChildAt(0).getMeasuredHeight();
            if(scrollContentHeight == realContentHeight) {
                return true;
            }
        }

        return false;
    }

    public static void scrollToBottom(final ScrollView scrollView) {
        if(scrollView != null) {
            scrollView.post(new Runnable() {
                public void run() {
                    scrollView.fullScroll(130);
                }
            });
        }

    }

    public static void scrollToBottom(final AbsListView absListView) {
        if(absListView != null && absListView.getAdapter() != null && ((ListAdapter)absListView.getAdapter()).getCount() > 0) {
            absListView.post(new Runnable() {
                public void run() {
                    absListView.setSelection(((ListAdapter)absListView.getAdapter()).getCount() - 1);
                }
            });
        }

    }

    public static void scrollToBottom(RecyclerView recyclerView) {
        if(recyclerView != null) {
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if(recyclerView.getAdapter() != null && recyclerView.getAdapter().getItemCount() > 0) {
                layoutManager.scrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
            }
        }

    }
}
